package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.enums.AppointmentEnum;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.RequestPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequestPermission(name="店铺业务页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
        model.addAttribute("pageInfo", appointmentService.query(qo));
        model.addAttribute("appointments",appointmentService.selectGroupByStatus());
        model.addAttribute("systemDictionaryItems",systemDictionaryItemService.selectByType(1));
        model.addAttribute("business",businessService.listAll());
        return "appointment/list";
    }


    @RequestMapping("/updateStatus")
    public String updateStatus(Long id,Integer status) {
            appointmentService.updateStatus(id,status);
        return "redirect:/appointment/list";
    }


    @RequestPermission(name="店铺业务删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }

    //前台新增
    @RequestPermission(name="前台业务新增",expression="appointment:save")
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Appointment appointment) {
        JsonResult result = new JsonResult();
        appointment.setStatus(AppointmentEnum.PEND.getValue());
        appointmentService.save(appointment);
        return result;
    }

    //后台新增编辑
    @RequestPermission(name="店铺业务新增/编辑",expression="appointment:update")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId()==null){
            appointment.setStatus(AppointmentEnum.PERFORM.getValue());
            appointmentService.save(appointment);
        }else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }


    @RequestPermission(name="店铺业务新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("appointment",appointmentService.get(id));
        }
        return "appointment/input";
    }
}
